package com.example.springboot.utils.TimerTask;



import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TimerHashMap implements Map<String,TaskInfo>{
    /*单例模式下堆内存只有一个静态的baseData极其容易在多线程环境下出现异常，上一把对象锁*/
    private final static HashMap<String, TaskInfo> baseData = new HashMap<>(20);

    public boolean endTaskInCache(String key,TaskInfo taskInfo){
        synchronized (baseData){
            if (baseData.containsKey(key)){
                boolean remove = baseData.remove(key,taskInfo);
                if (remove){
                    taskInfo.setTaskTime(0);
                    TaskInfo put = baseData.put(key, taskInfo);
                    return put == null;
                }
                return false;
            }
            return false;
        }
    }

    @Override
    public int size() {
        return baseData.size();
    }

    @Override
    public boolean isEmpty() {
        return baseData.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return baseData.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return baseData.containsValue(value);
    }

    @Override
    public TaskInfo get(Object key) {
        synchronized (baseData) {
            return baseData.get(key);
        }
    }


    @Override
    public TaskInfo put(String key, TaskInfo value) {
       synchronized (baseData){
           return baseData.put(key,value);
       }
    }

    @Override
    public TaskInfo remove(Object key) {
        synchronized (baseData){
            return baseData.remove(key);
        }
    }

    @Override
    public void putAll(Map<? extends String, ? extends TaskInfo> m) {
       synchronized (baseData){
            baseData.putAll(m);
       }
    }

    @Override
    public void clear() {
        synchronized (baseData){
            baseData.clear();
        }
    }


    @Override
    public Set<String> keySet() {
        synchronized (baseData){
            return baseData.keySet();
        }
    }


    @Override
    public Collection<TaskInfo> values() {
        synchronized (baseData){
            return baseData.values();
        }
    }


    @Override
    public Set<Entry<String, TaskInfo>> entrySet() {
        synchronized (baseData){
            return baseData.entrySet();
        }
    }

    /*测试能否正常运行计时任务*//*
    public static void main(String[] args) {
        TimerHashMap timerHashMap = new TimerHashMap();
        timerHashMap.put("task01",new TaskInfo("token",3*1000,TaskType.TASK_DELETE_CACHE));
        timerHashMap.put("task02",new TaskInfo("payCode",3*1000,TaskType.TASK_INTER_CACHE));
        new Thread(new Runnable() {
            @Override
            public void run() {
                TaskInfo task01 = timerHashMap.get("task01");
                System.out.println(task01.getTaskServerName()+"start");
                try {
                    Thread.sleep(task01.getTaskTime());
                    System.out.println(task01.getTaskServerName()+"remove");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (task01.getType() == TaskType.TASK_DELETE_CACHE) {
                    timerHashMap.remove("task01");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                TaskInfo task02 = timerHashMap.get("task02");
                System.out.println(task02.getTaskServerName()+"start");
                try {
                    Thread.sleep(task02.getTaskTime());
                    System.out.println(task02.getTaskServerName()+"remove");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (task02.getType() == TaskType.TASK_DELETE_CACHE) {
                    timerHashMap.remove("task01");
                }
                System.out.println(timerHashMap.get("task02").toString());
            }
        }).start();
    }*/
}

